package com.mybed.crm.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mybed.crm.anno.Log;
import com.mybed.crm.pojo.dto.PageTransferDto;
import com.mybed.crm.pojo.pojo.Business;
import com.mybed.crm.pojo.pojo.Clues;
import com.mybed.crm.pojo.pojo.User;
import com.mybed.crm.pojo.vo.MsgAndFlagVo;
import com.mybed.crm.result.PageResult;
import com.mybed.crm.result.Result;
import com.mybed.crm.service.BusinessService;
import com.mybed.crm.service.CluesService;
import com.mybed.crm.service.TransferService;
import com.mybed.crm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/transfer")
public class TransferController {
    @Autowired
    private TransferService transferService;
    @Autowired
    private CluesService cluesService;
    @Autowired
    private UserService userService;
    @Autowired
    private BusinessService businessService;

    /**
     * 分页查询转派管理
     * @param dto
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/list")
    public PageResult transferList(PageTransferDto dto,
                                   @RequestParam("params[beginTime]")String beginTime,
                                   @RequestParam("params[endTime]")String endTime){
        return transferService.page(dto,beginTime,endTime);
    }

    /**
     * 转派
     * @param status
     * @param userId
     * @param toUserId
     * @return
     */
    @Log("转派")
    @PutMapping("/assignment/{status}/{userId}/{toUserId}")
    public Result assignment(@PathVariable Integer status,
                             @PathVariable Integer userId,
                             @PathVariable Integer toUserId){
        User user = userService.getById(userId);
        User touser = userService.getById(toUserId);
        if(status==0){
            LambdaUpdateWrapper<Clues> wrapper=new LambdaUpdateWrapper<>();
            wrapper.set(Clues::getOwner,touser.getUserName()).set(Clues::getUpdateBy,touser.getUserName()).set(Clues::getUpdateTime, LocalDateTime.now()).eq(Clues::getOwner,user.getUserName());
            cluesService.update(wrapper);
        }else {
            LambdaUpdateWrapper<Business> wrapper1=new LambdaUpdateWrapper<>();
            wrapper1.set(Business::getOwner,touser.getUserName()).set(Business::getUpdateBy,touser.getUserName()).set(Business::getUpdateTime, LocalDateTime.now()).eq(Business::getOwner,user.getUserName());
            businessService.update(wrapper1);
        }
        MsgAndFlagVo vo=new MsgAndFlagVo();
        vo.setFlag(true);
        vo.setMsg("转派成功");
        return Result.success(vo);
    }

}
